package com.barbeque.mall.service.wx;

import com.barbeque.mall.bean.goods.MarketGoodsSpecification;
import com.barbeque.mall.bean.goods.MarketGoodsSpecificationExample;
import com.barbeque.mall.bean.wx.goods.SpecificationsVO;
import com.barbeque.mall.mapper.MarketGoodsSpecificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 苏东宇
 * @since 2022/07/21 15:17
 */
@Service
public class WxSpecificationServiceImpl implements WxSpecificationService {

    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    /**
     * 商品详情页获取规格信息
 * @param goodsId
     * @return java.util.List<com.barbeque.mall.bean.wx.goods.SpecificationsVO>
     * @author 苏东宇
     * @since 2022/07/21 15:44
     */
    @Override
    public List<SpecificationsVO> getSpecificationVO(Integer goodsId) {
        //先查询出该商品所有的规格
        MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria = marketGoodsSpecificationExample.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        criteria.andDeletedEqualTo(false);
        List<MarketGoodsSpecification> specifications = marketGoodsSpecificationMapper.selectByExample(marketGoodsSpecificationExample);

        //利用hashmap根据商品规格名称进行去重
        HashMap<String, MarketGoodsSpecification> map = new HashMap<>();
        for (MarketGoodsSpecification specification : specifications) {
            map.put(specification.getSpecification(),specification);
        }
        Set<String> keys = map.keySet();


        ArrayList<SpecificationsVO> specificationsVOS = new ArrayList<>();
        for (String key : keys) {
            SpecificationsVO specificationsVO = new SpecificationsVO();
            //根据规格名找出相同规格名的规格
            specificationsVO.setName(key);
            ArrayList<MarketGoodsSpecification> valueList = new ArrayList<>();
            for (MarketGoodsSpecification specification : specifications) {
                if (specification.getSpecification().equals(key)){
                    valueList.add(specification);
                }
                specificationsVO.setValueList(valueList);
            }
            specificationsVOS.add(specificationsVO);

        }
        return specificationsVOS;
    }
}
